package com.example.learningnote.advanced;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.example.learningnote.R;
import java.util.List;

/**
 * RecyclerView 适配器，包含联系人项和Footer
 */
class ContactAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_ITEM = 0; // 联系人项类型
    private static final int TYPE_FOOTER = 1; // Footer类型

    public static final int LOADING = 0; // 加载中状态
    public static final int LOADING_COMPLETE = 1; // 加载完成状态
    public static final int NO_MORE = 2; // 无更多数据状态

    private List<Contact> contactList; // 联系人数据
    private int loadState = LOADING_COMPLETE; // 当前加载状态
    private OnItemClickListener onItemClickListener; // 项点击监听器

    public ContactAdapter(List<Contact> contactList) {
        this.contactList = contactList;
    }

    /**
     * 设置Footer加载状态
     * @param state 加载状态
     */
    public void setLoadState(int state) {
        this.loadState = state;
        notifyItemChanged(getItemCount() - 1); // 刷新Footer
    }

    public interface OnItemClickListener {
        void onItemClick(Contact contact);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        return (position == getItemCount() - 1) ? TYPE_FOOTER : TYPE_ITEM;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_FOOTER) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_footer, parent, false);
            return new FooterViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.item_contact, parent, false);
            return new ContactViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ContactViewHolder) {
            Contact contact = contactList.get(position);
            ContactViewHolder contactHolder = (ContactViewHolder) holder;
            contactHolder.nameTextView.setText(contact.getName());
            contactHolder.phoneTextView.setText(contact.getPhone());
            contactHolder.itemView.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(contact);
                }
            });
        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerHolder = (FooterViewHolder) holder;
            switch (loadState) {
                case LOADING:
                    footerHolder.progressBar.setVisibility(View.VISIBLE);
                    footerHolder.textView.setVisibility(View.VISIBLE);
                    footerHolder.textView.setText("加载中...");
                    break;
                case LOADING_COMPLETE:
                    footerHolder.progressBar.setVisibility(View.GONE);
                    footerHolder.textView.setVisibility(View.GONE);
                    break;
                case NO_MORE:
                    footerHolder.progressBar.setVisibility(View.GONE);
                    footerHolder.textView.setVisibility(View.VISIBLE);
                    footerHolder.textView.setText("没有更多数据");
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return contactList.size() ; // 包含Footer
    }

    /**
     * 联系人项ViewHolder
     */
    static class ContactViewHolder extends RecyclerView.ViewHolder {
        TextView nameTextView;
        TextView phoneTextView;

        ContactViewHolder(View itemView) {
            super(itemView);
            nameTextView = itemView.findViewById(R.id.tvName);
            phoneTextView = itemView.findViewById(R.id.tvPhone);
        }
    }

    /**
     * Footer ViewHolder
     */
    static class FooterViewHolder extends RecyclerView.ViewHolder {
        ProgressBar progressBar;
        TextView textView;

        FooterViewHolder(View itemView) {
            super(itemView);
            progressBar = itemView.findViewById(R.id.progressBar);
            textView = itemView.findViewById(R.id.footerText);
        }
    }
}
